import time
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver


class ContactList:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:8080/client.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
    #登录状态正常搜索
    def SearchBoxByLogin(self):
        #搜索
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > input[type=text]").clear()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > input[type=text]").send_keys("zhangsan")
        #点击
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > button").click()
    #登录状态异常搜索
        #空白内容，点击搜索
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > input[type=text]").clear()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > input[type=text]")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > button").click()
        #输入内容，不点击搜索
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > input[type=text]").clear()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > input[type=text]").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search > button")
 #未登录状态
    def SearchTestByNotLogin(self):
        newurl = "http://127.0.0.1:8080/client.html"
        self.driver.get(newurl)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前用户未登录!"
        # 添加屏幕截图
        ChatDriver.getScreeShot()
        alert.accept()
        #回到登录界面
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 切换到警告框
        # 等待警告框出现
        try:
            WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
        except Exception as e:
            print(f"警告框未出现或已关闭: {e}")
        alert = self.driver.switch_to.alert
        alert.accept()